package com.photography.wedding.controller;

import com.photography.wedding.pojo.Menus;
import com.photography.wedding.pojo.News;
import com.photography.wedding.pojo.Picture;
import com.photography.wedding.service.MenusService;
import com.photography.wedding.service.NewsService;
import com.photography.wedding.service.PhotoService;
import com.photography.wedding.util.DateUtil;
import com.photography.wedding.util.ReturnUtil;
import com.photography.wedding.util.UuidUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RequestMapping
@RestController
public class MenusJsonController {
    @Autowired
    private MenusService menusService;

    @ResponseBody
    @RequestMapping(value = "/member/addMenus",method = RequestMethod.POST)
    public ModelMap addMenus(@Validated @RequestBody Menus menus, BindingResult result){
        try {

            if (menus.getJumpType() != null){
                menus.setJumpType(menus.getJumpType());
            }

            menus.setId(UuidUtil.getUUID());
            menus.setCreateTime(DateUtil.getCurrentTime());

            int count = menusService.insertSelective(menus);
            if (count == 0){
                return ReturnUtil.error("添加菜单失败");
            }

            return ReturnUtil.success("添加菜单成功");
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }

    }


    @ResponseBody
    @RequestMapping(value = "/member/selectMenusIdAll",method = RequestMethod.POST)
    public ModelMap selectMenusIdAll(@Validated @RequestBody Menus menus, BindingResult result){
        try {
            if(StringUtils.isEmpty(menus.getUserId())){
                return ReturnUtil.error("用户ID不能为空", null, null);
            }

            List<Menus> menusList = menusService.selectMenusIdAll(menus.getUserId());
            if (menusList.size() == 0){
                return ReturnUtil.error("暂无数据信息");
            }

            return ReturnUtil.success("查询菜单成功", menusList);
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }


    @ResponseBody
    @RequestMapping(value = "/member/deleteMenusByPrimaryKey",method = RequestMethod.POST)
    public ModelMap deleteMenusByPrimaryKey(@Validated @RequestBody Menus menus, BindingResult result){
        try {

            if(StringUtils.isEmpty(menus.getId())){
                return ReturnUtil.error("菜单ID不能为空", null, null);
            }

            int count = menusService.deleteByPrimaryKey(menus.getId());
            if (count == 0){
                return ReturnUtil.error("删除用户失败");
            }

            return ReturnUtil.success("删除用户成功");
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }


    @ResponseBody
    @RequestMapping(value = "/member/selectMenus",method = RequestMethod.POST)
    public ModelMap selectMenus(@Validated @RequestBody Menus menus, BindingResult result){
        try {
            if(StringUtils.isEmpty(menus.getId())){
                return ReturnUtil.error("文件ID不能为空", null, null);
            }

            Menus menus1 = menusService.selectByPrimaryKey(menus.getId());
            if (menus1 == null){
                return ReturnUtil.error("暂无数据信息");
            }

            return ReturnUtil.success("查询菜单成功", menus1);
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }


    @ResponseBody
    @RequestMapping(value = "/member/updateMenus",method = RequestMethod.POST)
    public ModelMap updateMenus(@Validated @RequestBody Menus menus, BindingResult result){
        try {
            if(StringUtils.isEmpty(menus.getId())){
                return ReturnUtil.error("文件ID不能为空", null, null);
            }

            if(StringUtils.isEmpty(menus.getModuleName())){
                return ReturnUtil.error("文件名称不能为空", null, null);
            }

            int count = menusService.updateByPrimaryKeySelective(menus);
            if (count == 0){
                return ReturnUtil.error("修改菜单失败");
            }

            return ReturnUtil.success("修改菜单成功");
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }

    }

}
